package controllers;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.CustomerService;
import services.GalleryService;
import services.PaintingService;
import domain.Customer;
import domain.Gallery;
import domain.Painting;

@Controller
@RequestMapping("/painting")
public class PaintingController extends AbstractController{

	// Services
	@Autowired
	private PaintingService paintingService;
	@Autowired
	private CustomerService customerService;
	@Autowired
	private GalleryService galleryService;

	// Constructor
	public PaintingController() {
		super();
	}

	// Listing methods
	@RequestMapping(value = "/galleryList", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam int galleryId) {
		ModelAndView result;
		Gallery gallery;
		Collection<Painting> paintings;

		gallery = galleryService.findOne(galleryId);
		paintings = paintingService.findByGallery(gallery);

		result = new ModelAndView("painting/galleryList");
		result.addObject("requestURI", "gallery/galleryList.do?galleryId=" + galleryId);
		result.addObject("paintings", paintings);
		result.addObject("backURI", "/customer/list.do");
		return result;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView listCustomer(@RequestParam int customerId) {
		ModelAndView result;
		Customer customer;
		Collection<Painting> paintings;

		customer = customerService.findOne(customerId);
		paintings = paintingService.findByCustomer(customer);

		result = new ModelAndView("painting/list");
		result.addObject("requestURI", "painting/list.do?customerId=" + customerId);
		result.addObject("paintings", paintings);
		result.addObject("backURI", "/customer/list.do");
		return result;
	}
}
